/*
 * NativeCallbacksDeclaration.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.delegate;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeGenerator;
import com.cute4j.tools.ng.Utils;

/**
 * Native Callbacks Declaration.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
class NativeCallbacksDeclaration extends NativeDeclarationBlock {

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new NativeCallbacksDeclaration object.
   *
   * @param nativeFile the abstract native file
   * @param text the text
   * @param javaClass the java class
   */
  public NativeCallbacksDeclaration(AbstractNativeFile nativeFile, String text,
                                    JavaClass javaClass) {
    super(nativeFile, text, javaClass);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public void store(PrintWriter pw) throws IOException {
    pw.println("public: // native callbacks");
    ArrayList textMethods = getMethodDeclarationsFromText();
    JavaMethod[] methods = getJavaClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
      JavaMethod method = methods[i];
      if (!method.isNative() && !method.isAbstract() &&
          method.getName().startsWith("_")) {
        String methodDeclaration = Utils.getCallbackMethodDeclaration(method);
        int mdi = textMethods.indexOf(methodDeclaration);
        if (mdi >= 0) {
          textMethods.remove(mdi);
        }
        pw.println(methodDeclaration);
      }
    }
    for (int i = 0; i < textMethods.size(); i++) {
      String methodText = (String)textMethods.get(i);
      if (methodText.startsWith("  ")) {
        String newLine = NativeGenerator.getInstance().getNewLine();
        methodText = methodText.replaceAll(newLine, newLine + "//");
        pw.print("//");
      }
      pw.println(methodText);
    }
  }

}

